function bmp2c( filename )

    f.fullname = filename;
    fprintf('Converting %s\n', filename);
    [f.path, f.name, f.ext] = fileparts(f.fullname);

    info = imfinfo(f.fullname);
    [img] = imread(f.fullname);

    if( info.ColorType ~= 'indexed' )
        error('Non indexed bitmap - not supported');
    else
        info.Greyscale = true;
        for i = 1:length(info.Colormap)
            if( (info.Colormap(i,1) ~= info.Colormap(i,2)) || (info.Colormap(i,2) ~= info.Colormap(i,3)) )
                info.Greyscale = false;
            end
        end
        if( info.Greyscale == false )
            if( length(info.Colormap) > 64 )
                info.Format = 2;
                info.PaletteEntries = 256;
            elseif( length(info.Colormap) > 8 )
                info.Format = 1;
                info.PaletteEntries = 64;
            else
                info.Format = 0;
                info.PaletteEntries = 8;
            end
        else
            info.Format = 128;
        end
    end

    fid = fopen( fullfile(f.path, [f.name '.c']), 'w' );
    fprintf(fid, '/*\nAutomatic conversion\n', filename );
    fprintf(fid, 'DO NOT EDIT THIS FILE DIRECTLY\n\n' );
    fprintf(fid, 'Filename: %s\n', info.Filename );
    fprintf(fid, 'FileModDate: %s\n', info.FileModDate );
    fprintf(fid, 'Format: %s\n', info.Format );
    fprintf(fid, 'FormatVersion: %s\n', info.FormatVersion );
    fprintf(fid, 'Width: %d\n', info.Width );
    fprintf(fid, 'Height: %d\n', info.Height );
    fprintf(fid, 'BitDepth: %d\n', info.BitDepth );
    fprintf(fid, '\nSee bmp2c.m matlab script for more information\n');
    fprintf(fid, '*/\n\n\n');
    fprintf(fid, '#include <avr/pgmspace.h>\n\n');
    fprintf(fid, 'const char bmp_%s[] PROGMEM = {\n', f.name);
    fprintf(fid, '\n\t\t%d, /* Format 0 = 8 color palette, 1 = 64 color palette, 2 = 256 color palette, 128 = greyscale */', info.Format);
    fprintf(fid, '\n\t\t%d, /* Width */', info.Width);
    fprintf(fid, '\n\t\t%d, /* Height */', info.Height);

    if( ~info.Greyscale )   % need to output the palette in the weird 565 format
        for y = 1:256
            %red			0b1111100000000000 (31)
            %green 			0b0000011111100000 (63)
            %blue			0b0000000000011111 (31)
            if( y <= length(info.Colormap) )
                r = uint16(info.Colormap(y,1) * 31);
                g = uint16(info.Colormap(y,2) * 63);
                b = uint16(info.Colormap(y,3) * 31);
            else 
                r = uint16(0);
                g = uint16(0);
                b = uint16(0);
            end
            %byte order might look a little odd here
            fprintf(fid, '\n\t\t0x%02X, 0x%02X,', ...
                bitand( 255, bitor( bitsll(g, 5), b ) ), ... 
                bitand( 255, bitor( bitsll(r, 3), bitsrl(g, 3) ) ) );
        end
    end


    % output is purely imgmap(img(i))
    c = 0;
    for y = 1:size(img,1)
        for x = 1:size(img,2)
            if( c == 0 ) 
                c = 16;
                fprintf(fid, '\n\t\t');
            end
            if( info.Greyscale )
                fprintf(fid, '0x%02X, ', 255*info.Colormap(img(y,x)+1,1) );  %still do the map lookup incase it's not in sequence
            else
                fprintf(fid, '0x%02X, ', img(y,x) );  %still do the map lookup incase it's not in sequence
            end
            c = c - 1;
        end
    end
    fprintf(fid, '\n};\n');
    
    fclose(fid);
